package com.boarsoft.boar.gateway.wechat.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.boarsoft.boar.gateway.wechat.biz.TempletBiz;
import com.boarsoft.boar.gateway.wechat.common.WechatConfig;
import com.boarsoft.boar.gateway.wechat.common.WechatConstance;
import com.boarsoft.cache.Cache;
import com.boarsoft.wechat.entity.WeChatTemplate;
import com.boarsoft.wechat.service.TempletService;
import com.google.gson.Gson;

@Component("templetService")
public class TempletServiceImpl implements TempletService {

	private Logger log = LoggerFactory.getLogger(TempletServiceImpl.class);
	
	@Autowired
	private Cache cache;
	
	@Autowired
	private TempletBiz templetBiz;

	@Override
	public Map<String, Object> upadteTemplet(String dealFlay, WeChatTemplate template) throws Exception {
		//检查模板是否存在
		log.info("更新消息模板|{}|{}", dealFlay, new Gson().toJson(template));
		Map<String, Object> reMap = new HashMap<String, Object>();
		String fakeId = WechatConfig.getProperty("weixin.platformFakeId");
		if(fakeId.equals(template.getPlatformFakeId())){
			if("1".equals(dealFlay)){
				cache.put(WechatConstance.TEMPLET_CACHE_NAME, template.getCode(), template);
			}else if("2".equals(dealFlay)){
				cache.put(WechatConstance.TEMPLET_CACHE_NAME, template.getCode(), template);
			}else if("3".equals(dealFlay)){
				cache.remove(WechatConstance.TEMPLET_CACHE_NAME, template.getCode());
			}
			reMap.put("respCode", "0000000000");
			reMap.put("respMsg", "成功");
		}else{
			log.info("更是失败,模板公众号不一致|{}", fakeId);
			reMap.put("respCode", "EBWX050001");
			reMap.put("respMsg", "模板公众号不一致");
		}
		return reMap;
	}

	@Override
	public Map<String, Object> upadteMsgCode(String dealFlay, String msgCode, String tempId) throws Exception {
		//检查模板是否存在
		log.info("更新消息模板|{}|{}|{}", dealFlay, msgCode, tempId);
		Map<String, Object> reMap = new HashMap<String, Object>();
		WeChatTemplate template = templetBiz.getTemplateById(tempId);
		if(template != null){
			String fakeId = WechatConfig.getProperty("weixin.platformFakeId");
			if(fakeId.equals(template.getPlatformFakeId())){
				String tempCode = template.getCode();
				if("1".equals(dealFlay)){
					cache.put(WechatConstance.TEMPLET_TYPE_CACHE_NAME, tempCode + "_" + msgCode, tempCode);
				}else if("2".equals(dealFlay)){
					cache.put(WechatConstance.TEMPLET_TYPE_CACHE_NAME, tempCode + "_" + msgCode, tempCode);
				}else if("3".equals(dealFlay)){
					cache.remove(WechatConstance.TEMPLET_TYPE_CACHE_NAME, tempCode + "_" + msgCode);
				}
				reMap.put("respCode", "0000000000");
				reMap.put("respMsg", "成功");
			}else{
				log.info("更是失败,模板公众号不一致|{}", fakeId);
				reMap.put("respCode", "EBWX050001");
				reMap.put("respMsg", "模板公众号不一致");
			}
		}else{
			log.info("模板不存在");
			reMap.put("respCode", "EBWX050002");
			reMap.put("respMsg", "模板不存在");
		}
		return reMap;
	}

}
